﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Web.PayChannal.Code;
using System.Net;
using System.Collections;
using System.Text;
using ZLCMS.Business.Util;
using System.IO;
using System.Xml;
using ZLCMS.Business.WeixinService;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using System.Text.RegularExpressions;
using ZLCMS.Data.Model;
using ZLCMS.Commands.Member.Commands.Member;
using DotNet.Common.Command;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP;
using Newtonsoft.Json.Linq;

namespace ZLCMS.Web.PayChannal.Weixinpay
{
    public partial class PaymentRequire : PaymentRequireBase
    {
        protected string prepayId { get; set; }

        //jsapi启动时所需的参数
        protected Hashtable choosePayParamHash = new Hashtable();

        protected void Page_Load(object sender, EventArgs e)
        {
            //不是使用微信浏览器打开
            if (!WeixinUtil.IsWeixinBroswer())
            {
                Server.Transfer("/PayChannal/Weixinpay/PaymentRequireNative.aspx?pno=" + PaymentNo + "&pid=" + PaymentInfo.Id);
            }
            //使用跨号支付标识
            if (PaymentNo.IndexOf("_NATIVE") >= 0)
            {
                Server.Transfer("/PayChannal/Weixinpay/PaymentRequireNative.aspx?pno=" + PaymentNo + "&pid=" + PaymentInfo.Id);
            }

            //支付类型不正确
            if (PaymentInfo.InterfaceId != "WXPAY")
            {
                ReportError("支付方式异常！");
                return;
            }

            //当前用户没有绑定微信openid，则需要使用扫码支付
            if (string.IsNullOrEmpty(CurrentMember.WxOpenId))
            {
                Server.Transfer("/PayChannal/Weixinpay/PaymentRequireNative.aspx?pno=" + PaymentNo + "&pid=" + PaymentInfo.Id);
            }


            Hashtable paramHash = new Hashtable();
            paramHash["appid"] = PaymentInfo.AppId;
            paramHash["mch_id"] = PaymentInfo.UserId;		  //商户号
            paramHash["trade_type"] = "JSAPI";
            paramHash["nonce_str"] = GetNoncestr();
            paramHash["openid"] = CurrentMember.WxOpenId;
            paramHash["out_trade_no"] = PaymentNo;
            paramHash["total_fee"] = ((int)(PaymentPrice * 100)).ToString();
            paramHash["notify_url"] = ServerUtil.GetRootURI() + "/PayChannal/Weixinpay/PaymentCallback.aspx";
            paramHash["body"] = ProductDesc;
            paramHash["spbill_create_ip"] = Request.UserHostAddress;
            paramHash["time_start"] = string.Format("{0:yyyyMMddHHmmss}", OrderTime);
            paramHash["time_expire"] = string.Format("{0:yyyyMMddHHmmss}", ExpireTime);

            //签名
            WXPayUtil.SetMD5Sign(paramHash, PaymentInfo.PrivateKey);

            
            Hashtable responseHash = WXPayUtil.GetResponseHash("wxorder_" + PaymentNo, "https://api.mch.weixin.qq.com/pay/unifiedorder", paramHash);
            

            prepayId = "";
            if (responseHash["return_code"] as string == "SUCCESS" && responseHash["result_code"] as string == "SUCCESS")
            {
                prepayId = responseHash["prepay_id"] as string;
            }
            //调用接口出错     
            else
            {
                Response.Write(responseHash["return_msg"] + "=>" + responseHash["err_code_des"]);
                Response.End();
                return;
            }
            //调用接口失败，未知错误
            if (string.IsNullOrEmpty(prepayId))
            {
                Response.Write("服务器忙");
                Response.End();
                return;
            }

            choosePayParamHash["appId"] = PaymentInfo.AppId;
            choosePayParamHash["timeStamp"] = string.Format("{0}", GetTimestamp());
            choosePayParamHash["nonceStr"] = GetNoncestr();
            choosePayParamHash["package"] = "prepay_id=" + prepayId;
            choosePayParamHash["signType"] = "MD5";
            //签名
            WXPayUtil.SetMD5Sign(choosePayParamHash, PaymentInfo.PrivateKey);


        }

    }
}